10 map
<int, set
<string
> > dict
;
11 map
<char, char> solucion
;
14 bool contradice(const string
&origen
, const string
&destino
, map
<char, char> crypt
){
15 if (origen
.length() != destino
.length()) return true;
17 for (int i
=0; i
<origen
.size(); ++i
){
18 if (crypt
.count(origen
[i
]) == 1){
19 if (crypt
[origen
[i
]] != destino
[i
]){
23 for (map
<char, char>::iterator j
= crypt
.begin(); j
!= crypt
.end(); ++j
){
24 if ((*j
).second
== destino
[i
]) return true;
26 crypt
[origen
[i
]] = destino
[i
];
32 map
<char, char> asignar(const string
&origen
, const string
&destino
, map
<char, char> crypt
){
33 if (origen
.size() == destino
.size()){
34 for (int i
=0; i
<origen
.size(); ++i
){
35 crypt
[origen
[i
]] = destino
[i
];
41 bool bt(const int &i
, const vector
<string
> &words
, map
<char, char> crypt
){
42 if (i
>= words
.size()){
47 int len
= words
[i
].length();
48 set
<string
> possible
= dict
[len
];
50 for (set
<string
>::iterator j
= possible
.begin(); j
!= possible
.end(); ++j
){
51 if (!contradice(words
[i
], (*j
), crypt
)){
52 if (bt(i
+1, words
, asignar(words
[i
], (*j
), crypt
))){
68 dict
[s
.length()].insert(s
);
72 while (getline(cin
, line
)){
80 stringstream
ss(line
);
83 words
.push_back(word
);
86 if (bt(0, words
, map
<char, char>() )){
87 for (int i
=0; i
<line
.size(); ++i
){
88 if (line
[i
] == ' ') cout
<< " ";
89 else cout
<< solucion
[line
[i
]];
93 for (int i
=0; i
<line
.size(); ++i
){
94 if (line
[i
] == ' ') cout
<< " ";